!function(){
	var _this = function(callback){
		if (typeof(callback)=="string") {
			return _this.require(callback);
		} else if(typeof(callback)=="function"){
			_this.ready(callback);
		}
	};
	_this.apiReadyed=false;
	_this.ready = function(callback){
		if (_this.apiReadyed) {
			typeof(callback)=="function" && callback();
		}else{
			typeof(callback)=="function" && _this.ready.callbackList.push(callback);
		}
	}
	_this.ready.callbackList = [];
	window['apiready'] = function() {
		_this.apiReadyed = true;
		_this.isAndroid = api.systemType=='android';
		_this.isIOS = api.systemType=='ios';
		for (;0<_this.ready.callbackList.length;) {
			_this.ready.callbackList[0]();
			_this.ready.callbackList.splice(0,1);
		}
	};
	_this.require = function(name){
		_this.require.modules[name] = _this.require.modules[name] || api.require(name);
		return _this.require.modules[name];
	}
	_this.require.modules = {};
	_this.ajax = function(setup){
		setup = setup || {};
		setup.type = setup.type ? setup.type.toLowerCase() : 'get';
		if(!setup.url) {return false;};
		setup.headers = setup.headers || {};
		setup.headers['X-Requested-With']='XMLHttpRequest';
		//data处理
		if(setup.data){
			var dataTypeIndex = ['String','Array','Object'].indexOf($.type(setup.data));
			if(dataTypeIndex === -1) throw '发送数据类型错误';
			if (setup.type == 'get') {
				setup.url += (setup.url.indexOf('?') === -1?'?':'&') + $.param(setup.data);
				setup.data=null;
			}else if(setup.file){
				setup.data = {
					values: dataTypeIndex==0?$.reparam(setup.data):setup.data,
					files:setup.file
				}
			}else{
				setup.headers['Content-Type'] = setup.headers['Content-Type'] || 'application/x-www-form-urlencoded';
				setup.data = {body:$.param(setup.data)};
			}
		}
		setup.beforeSend && setup.beforeSend(xhr);
		api.ajax({
			url: setup.url,
			method: setup.type,
			returnAll: true,
			headers: setup.headers,
			data: setup.data
		}, function (ret, err) {
			if(ret){
				setup.success && setup.success(ret.body,ret);
			}else{
				setup.error && setup.error(err);
			}
			setup.complete && setup.complete(ret);
		});
	}
	_this.cache = function(name, value){
		if (typeof(value)=="undefined") {
			var str = api.getPrefs({sync: true,key: name});
			return str ? JSON.parse(str)['cache'] : false;
		} else{
			var data = {cache: value};
			api.setPrefs({key: name,value: JSON.stringify(data)});
		}
	}
	/**
	 * app专属eval方法
	 * @param {JSString} jsstr 执行的js代码，和eval方法类似
	 * @param {String} name 代码执行的窗口name，不传为当前窗口，初始窗口为"root"，支持使用点二级参数指定Frame，如："root.menu"
	 */
	_this.eval = function(jsstr, name){
		var setup={};
		if(name && name.indexOf('.')!=-1){
			name = name.split('.');
			setup={name: name[0],frameName:name[1],script: jsstr};
		}else{
			setup={name: name,script: jsstr};
		}
		api.execScript(setup);
	}
	_this.win = function(name, op){
		//第二参数传入否表示关闭window
		if (op===false) {
			return api.closeWin(name ? {name:name} : {});
		} else {
			op = (typeof(op)=="object") ? op : {};
			var setup={};
			if(/^https?::\/\//.test(name)){
				setup.name = name;
				setup.url = name;
				setup.reload = true;//网页默认刷新
			}else{
				if(name.indexOf('?')>0){
					var arr = name.split('?');
					name = arr[0];
					setup.pageParam = $.reparam(arr[1]);
					if (op.pageParam) {
						setup.pageParam = $.merge(setup.pageParam, op.pageParam);
						delete op.pageParam;
					}
				}
				var url = 'widget://html/'+name+'.html';
				setup.url = url;
			}
			setup.name = name;
			api.openWin($.merge(setup,op));
		}
	}
	_this.frame = function(name, op){
		//第二参数传入否表示关闭frame
		if (op===false) {
			return api.closeFrame(name ? {name:name} : {});
		} else {
			op = (typeof(op)=="object") ? op : {};
			var setup={};
			if(/^https?::\/\//.test(name)){
				setup.url = name;
				setup.reload = true;//网页默认刷新
			}else{
				if(name.indexOf('?')>0){
					var arr = name.split('?');
					name = arr[0];
					setup.pageParam = $.reparam(arr[1]);
					if (op.pageParam) {
						setup.pageParam = $.merge(setup.pageParam, op.pageParam);
						delete op.pageParam;
					}
				}
				var url = 'widget://html/'+name+'.html';
				setup.url = url;
			}
			setup.name = name;
			api.openFrame($.merge(setup,op));
		}
	}
	
	window['app'] = _this;
	if (window['$']) {
		window['$']['app'] = _this;
		window['$']['ajax']  = _this.ajax;
	}
}()